package com.quyunshuo.module.home.fragment.home

import android.os.Bundle
import android.util.SparseArray
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.quyunshuo.androidbaseframemvvm.common.constant.KEY_ID
import com.quyunshuo.androidbaseframemvvm.network.bean.ProjectTabItem
import com.quyunshuo.module.home.fragment.project.ProjectFragment
import com.quyunshuo.module.home.fragment.video.VideoFragment

class ViewPagerAdapter(
    fragmentManager: FragmentManager,
    lifecycle: Lifecycle,
    var fragments: MutableList<ProjectTabItem>

) :
    FragmentStateAdapter(
        fragmentManager, // 关键点：使用子 FragmentManager
        lifecycle
    ) {// 绑定视图生命周期

    override fun getItemCount(): Int = fragments.size

    override fun createFragment(position: Int): Fragment {
        return when (position) {
            1 -> ProjectFragment()
            else -> VideoFragment.newInstance(fragments[position].id)
        }
    }

    fun setData(fragments:MutableList<ProjectTabItem>) {
        this.fragments = fragments
    }
}
